﻿// <copyright file="Pox.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>POX serializer for Square and King</summary>

namespace Sqwarea.Api.Web.Misc
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Xml.Linq;
    using Sqwarea.GameLogic.Extensions;
    using Sqwarea.Persistence;

    /// <summary>
    /// POX Serializer for Square and King
    /// </summary>
    public static class Pox
    {
        /// <summary>
        /// Serialize a square in POX
        /// Just output the position (x,y) and the kingId
        /// </summary>
        /// <param name="square">the square to serialize</param>
        /// <returns>POX stream</returns>
        public static XElement ToPox(this Square square)
        {
            var element = new XElement(
                "square",
                new XElement("x", square.Position.X),
                new XElement("y", square.Position.Y),
                new XElement("king", square.KingId));
            return element;
        }

        /// <summary>
        /// Serialize a king in POX
        /// Just output the id, the nickname and the squares number
        /// </summary>
        /// <param name="king">the king to serialize</param>
        /// <returns>POX stream</returns>
        public static XElement ToPox(this King king)
        {
            // We save Id because IsNeutral can change Id
            var id = king.Id;
            var neutral = king.IsNeutral();

            var element = new XElement(
                "king",
                new XElement("id", id),
                new XElement("neutral", neutral),
                new XElement("nickname", neutral ? null : king.Nickname),
                new XElement("squares", neutral ? null : (int?)king.SquaresNumber));

            return element;
        }
    }
}
